// pages/login/login.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        password: '',
        phonenumber: '',
        errorMessagepn: '',
        errorMessagepw: '',
        errorMessagevc: '',
        verificationcode: '',
        text: ''
    },
    getPhoneNumber(e) {
        let number = e.detail.value;
        if (number == '') {
            this.setData({
                errorMessagepn: ''
            })
        }
        if (!this.checkValue(number)) {
            this.setData({
                errorMessagepn: '手机号格式有误！'
            })
        } else {
            this.setData({
                errorMessagepn: ''
            })
        }
        this.setData({
            phonenumber: e.detail.value
        })
    },
    checkValue(number) {
        let reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
        return reg.test(number)
    },
    getPassword(e) {
        if (e.detail.value == '') {
            this.setData({
                errorMessagepw: '请输入密码！'
            })
        } else {
            this.setData({
                password: e.detail.value,
                errorMessagepw: ''
            })
        }

    },
    signin() {
        if (this.data.verificationcode.toLowerCase() !== this.data.text.toLowerCase()) {
            Toast({ message: '验证码错误！', mask: true, position: 'top' });
        } else if (this.data.phonenumber == '' || this.data.password == '') {
            Toast({ message: '请输入手机号或密码！', mask: true, position: 'top' });
        } else if (!this.checkValue(this.data.phonenumber)) {
            Toast({ message: '请输入正确的手机号！', mask: true, position: 'top' });
        }
        else {
            wx.request({
                url: `${config.baseUrl}/login`,
                method: 'POST',
                data: {
                    phonenumber: this.data.phonenumber,
                    password: this.data.password
                },
                success: (res) => {
                    let data = res.data;
                    const loginSus = () => {
                        return new Promise((resolve, reject) => {
                            resolve();
                        })
                    }
                    loginSus().then(() => {
                        if (data.code == 200) {
                            console.log(data.data);
                            let userInfo= Object.assign({isLogin:true},data.data)
                            wx.setStorage({key:'userInfo', data:userInfo});
                                wx.showToast({
                                  title: '登录成功！',
                                  duration:1500
                                })
                             setTimeout(()=>{
                                wx.reLaunch({
                                    url: '/pages/index/index'
                                })
                             },1500)
                            
                        }else{
                            wx.showToast({
                              title: data.msg,
                            })
                        }
                    }).catch(function(error) {
                        console.error(error); // 如果发生错误，输出错误信息
                      });


                }
            })
        }

    },
    getCaptcha(e) {
        console.log(e.detail.value);
    },
    toRegister() {
        wx.navigateTo({
            url: '/pages/register/register',
        })
    },
    getVerifyCode(e) {
        this.setData({
            verificationcode: e.detail.verificationcode,
            text: e.detail.text
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.hideHomeButton();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})